package com.situ.company.department.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.DepartmentService;
import com.situ.company.util.FmtRequest;

//控制层
//MVC
//model view controller
//servlet
//注释
//@注解
@WebServlet("/department") // @WebServlet规则：不同Servlet文件中WebServlet注解内容不能相同
public class DepartmentServlet extends HttpServlet {

    // 接受请求 获取参数 封装对象 调用方法 返回结果

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        Object res = "";
        switch (action) {
            case "list":
                res = list(req);
                break;
            case "page":
                res = page(req);
                break;
            case "add":
                res = add(req);
                break;
            case "del":
                res = del(req);
                break;
            case "get":
                res = get(req);
                break;
            case "upd":
                res = upd(req);
                break;
            case "get2":
                get2(req);
                req.getRequestDispatcher("/web/page/department/upd2.jsp").forward(req, resp);
                return;
        }
        FmtRequest.write(resp.getWriter(), res);
    }

    private void get2(HttpServletRequest req) {
        DepartmentModel model = new DepartmentModel(req.getParameter("code"));
        DepartmentModel mdb = service.selectModel(model);
        req.setAttribute("mdb", mdb);
    }

    private String upd(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        fields.put("tel", "tel");
        DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class, fields);
        // DepartmentModel model = FmtRequest.parseModel(req,
        // DepartmentModel.class);
        return service.update(model);
    }

    private DepartmentModel get(HttpServletRequest req) {
        DepartmentModel model = new DepartmentModel(req.getParameter("code"));
        return service.selectModel(model);
    }

    private String del(HttpServletRequest req) {
        DepartmentModel model = new DepartmentModel(req.getParameter("code"));
        return service.delete(model);
    }

    private String add(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        fields.put("tel", "tel");
        DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class, fields);
        return service.insert(model);
    }

    private List<DepartmentModel> list(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class, fields);
        return service.selectList(model);
    }

    private Map<String, Object> page(HttpServletRequest req) {
        Map<String, String> fields = new HashMap<>();
        fields.put("code", "code");
        fields.put("name", "name");
        DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class, fields);
        model.setPageIndex(Integer.parseInt(req.getParameter("pageIndex")));
        model.setPageLimit(Integer.parseInt(req.getParameter("pageLimit")));
        model.setPageOn(true);
        Map<String, Object> map = new HashMap<>();
        map.put("data", service.selectList(model));
        map.put("count", service.selectCount(model));
        return map;
    }

    private DepartmentService service = new DepartmentService();

}
